# @Time:2021/6/11 19:54
# @Author:5120195554王欣霏
# -*- coding = utf-8 -*-

import os
import enum
import pygame
import actor


# 孙悟空打斗时的状态
class SwkBattleStatus(enum.IntEnum):
    Station = 0  # 静止状态
    Fight = 1  # 打斗状态
    Escape = 2  # 逃跑开始
    EscapeOver = 3  # 逃跑结束
    Die = 4  # 死亡
    DieOver = 5  # 死完


# 孙悟空打斗时的精灵类
class SwkBattle(pygame.sprite.Sprite):
    def __init__(self, hp: int):  # hp是血量
        pygame.sprite.Sprite().__init__()
        # 以下资源都不循环播放
        self.magic_fight = actor.DirAction('magic', 'appear', '0000-b788e5a-', 1, 10, False)
        self.magic_die_escape = actor.DirAction('magic', 'disappear', '0027-760b7bd7-', 1, 10, False)
        self.station = actor.DirAction('swk', '', '', 4, 4, False)
        self.status = SwkBattleStatus.Station  # 一开始是静止状态
        self.hp = hp  # 初始化血量
        self.pos_x = 100
        self.pos_y = 60

    def attack_hp(self, hp):
        self.hp += hp
        if self.hp <= 0:
            self.set_status(SwkBattleStatus.Die)

    def set_status(self, status):  # 改变状态
        self.status = status
        if self.status == SwkBattleStatus.Fight:
            self.magic_fight.reset()
        elif self.status == SwkBattleStatus.Station:
            self.station.reset()
        elif self.status == SwkBattleStatus.Die:
            self.magic_die_escape.reset()
        elif self.status == SwkBattleStatus.Escape:
            self.magic_die_escape.reset()

    def action_over(self):  # 判断帧序列是否到达最后一帧
        if self.status == SwkBattleStatus.Station:
            return self.station.is_end()
        elif self.status == SwkBattleStatus.Fight:
            return self.magic_fight.is_end()
        elif self.status == SwkBattleStatus.Escape:
            if self.magic_die_escape.is_end():
                self.status = SwkBattleStatus.EscapeOver
            return self.magic_die_escape.is_end()
        elif self.status == SwkBattleStatus.Die:
            if self.magic_die_escape.is_end():
                self.status = SwkBattleStatus.DieOver
            return self.magic_die_escape.is_end()

    def draw(self, surface: pygame.Surface):
        dir = 0  # 没有方向 就直接设置
        if self.status == SwkBattleStatus.Station:
            image = self.station.get_curr_frame(dir)
            surface.blit(image, (self.pos_x, self.pos_y))
        elif self.status == SwkBattleStatus.Fight:
            image = self.station.get_curr_frame(dir)
            surface.blit(image, (self.pos_x, self.pos_y))
            image = self.magic_fight.get_curr_frame(0)
            surface.blit(image, (self.pos_x, self.pos_y))

        elif self.status == SwkBattleStatus.Die:
            image = self.magic_die_escape.get_curr_frame(0)
            surface.blit(image, (self.pos_x, self.pos_y))

        elif self.status == SwkBattleStatus.DieOver:
            image = self.magic_die_escape.get_curr_frame(0)
            surface.blit(image, (self.pos_x, self.pos_y))

        elif self.status == SwkBattleStatus.Escape:
            image = self.magic_die_escape.get_curr_frame(0)
            surface.blit(image, (self.pos_x, self.pos_y))

        elif self.status == SwkBattleStatus.EscapeOver:
            image = self.magic_die_escape.get_curr_frame(0)
            surface.blit(image, (self.pos_x, self.pos_y))

        pygame.draw.rect(surface, pygame.Color(255, 255, 255), pygame.Rect(150, 60, self.hp / 2, 5))  # 矩形区域
